package com.qf.service.impl;

import com.qf.dao.DruginfoRepository;
import com.qf.pojo.Druginfo;
import com.qf.pojo.PageUtils;
import com.qf.service.DruginfoService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Optional;

@Service
public class DruginfoServiceImpl implements DruginfoService {
    @Autowired
    DruginfoRepository druginfoRepository;

    @Override
    public PageUtils findAll(Integer page, Integer size) {
        PageRequest pageRequest = new PageRequest(page-1,size);
        Page<Druginfo> all = druginfoRepository.findAll(pageRequest);
        PageUtils pageUtils  = new PageUtils<>();
        if(all!=null){
            List<Druginfo> content = all.getContent();//获取当前页面所有数据
            pageUtils.setList(content);
            long totalElements = all.getTotalElements();//总共有多少条数据
            pageUtils.setTotal(totalElements);
            return pageUtils;
        }else {
        return null;
        }
    }

    @Override
    public Druginfo findById(String id) {
        Optional<Druginfo> byId = druginfoRepository.findById(id);
        if (byId!=null){
          return byId.get();
        }
        return null;
    }

    @Override
    public void insertAndUpdate(Druginfo druginfo) {
        druginfoRepository.saveAndFlush(druginfo);
    }

    @Override
    public void delete(String id) {
        druginfoRepository.deleteById(id);
    }
}
